

#include "StringLib.h"

char* StringCopy(char* des, const char* src)
{
    assert(des && src);
    int i = 0;
    while (*(src + i) != '\0')
    {
        *(des + i) = *(src + i);
        i++;
    }
    *(des + i) = '\0';
    return des;
}

int StringCompare(const char* str1, const char* str2)
{
    assert(str1 && str2);
    while (*str1 == *str2)
    {
        if (*str1 == '\0')
        {
            return 0;
        }
        str1++;
        str2++;
    }
    return *str1 - *str2;
}

int StringLength(const char* str)
{
    if (str == nullptr)
        return 0;

    int count = 0;
    while (*str != '\0')
    {
        count++;
        str++;
    }
    return count;
}

char* StringLink(char* left, const char* right)
{
    assert(left && right);
    int TotalLen = StringLength(left) + StringLength(right);
    int SpaceLen = sizeof(left) / sizeof(left[0]);
    if (TotalLen < SpaceLen)
        return nullptr;

    char* ans = left;

    while (*left)
    {
        left++;
    }

    while ((*left++ = *right++));
    return ans;
}